<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class TransferForm extends CFormModel {

    public $title;
    public $receiver_name;
    public $receiver_address;
    public $receiver_account;
    public $amount;
    public $cash_id;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('title,receiver_name,receiver_address,receiver_account,amount', 'required', 'message' => 'Pole wymagane'),
            array('receiver_account', 'length', 'is' => 26, 'message' => 'Numer konta musi mieć 26 znaków'),
            array('receiver_account', 'type', 'type' => 'integer', 'message' => 'Nie poprawny numer konta'),
            array('amount', 'type', 'type' => 'float', 'message' => 'Nie poprawna kwota przelewu'),
            array('amount', 'numerical', 'min' => 0.5, 'max' => 9999999999, 'tooSmall' => 'Kwota za mała', 'tooBig' => 'Kwotaa za duża'),
            array('amount', 'numerical', 'min' => 0.5, 'max' => 9999999999, 'tooSmall' => 'Kwota za mała', 'tooBig' => 'Kwotaa za duża'),
            array('cash_id', 'required', 'on' => 'outside', 'message' => 'Pole wymagane'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'title' => 'Tytuł przelewu',
            'receiver_name' => 'Adresat',
            'receiver_address' => 'Adresat część druga',
            'receiver_account' => 'Numer konta',
            'amount' => 'Kwota',
            'cash_id' => 'Waluta'
        );
    }
    
     public function addInnerTransfer($account, $userId){
        $result = Yii::app()->db->createCommand("SELECT sendtransferfun('".$this->amount."','".$this->title."','".$account."','".$userId."','".$this->receiver_account."','".$this->receiver_name.' '.$this->receiver_address."')")->queryAll();
        return $result[0]['sendtransferfun'];
    }
    
    public function addOutTransfer(){
        $result = Yii::app()->db->createCommand("SELECT outsidetransferfun('".$this->amount."','".$this->title."', '".$this->receiver_account."','".$this->receiver_name.' '.$this->receiver_address."', 'Przelew zewnętrzny', '00000000000000000000000000',  '".$this->cash_id."')")->queryAll();
        return $result[0]['outsidetransferfun'];
    }

}
